package com.codesniper.holderapp;

import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.codesniper.holderapp.logInfo.LogInfoItem;
import com.codesniper.holderapp.logInfo.LogInfoManager;
import com.codesniper.holderapp.test.GenerateCode;
import com.github.codesniper.poplayer.PopLayerView;

import java.util.ArrayList;

public class MainActivity extends Activity implements LogInfoManager.OnLogCatchListener{
    PopLayerView<Dialog> mLayerView;
    ListView listView;
    ArrayAdapter<LogInfoItem> arrayAdapter;
    private ArrayList<LogInfoItem> arrayList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        listView=findViewById(R.id.ll);
        /**
         * flavor 生成代码
         */
        Button button=findViewById(R.id.btn);
        button.setText(new GenerateCode().msg);

        NotificationUtils.showNotification(this);

        Toast.makeText(this,"onCreate",Toast.LENGTH_SHORT).show();

        LogInfoManager.getInstance().registerListener(this);
        LogInfoManager.getInstance().start();

        arrayList=new ArrayList<>();
        arrayAdapter=new ArrayAdapter<>(this,R.layout.tv,arrayList);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogInfoManager.getInstance().stop();
        LogInfoManager.getInstance().removeListener();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Toast.makeText(this,"onNewIntent",Toast.LENGTH_SHORT).show();
    }

    /**
     * poplayer
     */
    private void showPop() {
        mLayerView = new PopLayerView(this,R.layout.dialog_test);
        mLayerView.setOnPopDismissListener(new PopLayerView.onPopDismissListener() {
            @Override
            public void onDismiss() {
                showToast("onDismiss");
            }
        });
        mLayerView.show();
    }


    public void recyclePop(View view){
        mLayerView.recycle();
    }

    private void showToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    /**
     * 销毁目标Activity和它之上的所有Activity，重新创建目标Activity
     * @param view
     */
    public void second(View view){
        Log.d("TAURUS","second");
//        Intent intent=new Intent(this,SecondActivity.class);
//        startActivity(intent);
    }

    @Override
    public void onLogCatch(LogInfoItem infoItem) {
        arrayList.add(infoItem);
        arrayAdapter.notifyDataSetChanged();
    }
}
